package com.nrgedge.magnolia.form.processors;

import info.magnolia.context.MgnlContext;
import info.magnolia.module.ModuleRegistry;
import info.magnolia.module.form.processors.AbstractFormProcessor;
import info.magnolia.module.form.processors.FormProcessorFailedException;

import java.util.Map;

import javax.jcr.Node;
import javax.servlet.http.HttpServletRequest;

import com.nrgedge.magnolia.ReCaptchaModule;

public class ReCaptchaFormProcessor extends AbstractFormProcessor {

	@SuppressWarnings("deprecation")
	@Override
	protected void internalProcess(Node content, Map<String, Object> parameters) throws FormProcessorFailedException {
		final ReCaptchaModule module = (ReCaptchaModule) ModuleRegistry.Factory.getInstance().getModuleInstance("recaptcha");
		final HttpServletRequest request = MgnlContext.getWebContext().getRequest();
        
        if (!module.isFieldValidation() && !module.validateRequest(request)) {
        	throw new FormProcessorFailedException("ReCaptchaFormProcessor.errorMessage");
        }
	}

}
